unit unitselectdatabase;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, BCPanel, BCLabel, Forms, Controls, Graphics,
  Dialogs, StdCtrls, Buttons, ActnList, ExtCtrls, INIFiles, unitLanguage;

const strDatabase:String =  'analises';

type

  { TFormSelectDatabase }

  TFormSelectDatabase = class(TForm)
    ActionApply: TAction;
    ActionClearMineral: TAction;
    ActionClearSample: TAction;
    ActionNewMineralDatabase: TAction;
    ActionRefreshEdits: TAction;
    ActionFormClose: TAction;
    ActionSelectMineralDatabase: TAction;
    ActionSelectSampleDatabase: TAction;
    ActionNewSampleDatabase: TAction;
    ActionList1: TActionList;
    BCLabelSampleDB: TBCLabel;
    BCPanel2: TBCPanel;
    BCPanel3: TBCPanel;
    Edit2: TEdit;
    OpenDialog1: TOpenDialog;
    ScrollBox1: TScrollBox;
    SpeedButtonApply: TSpeedButton;
    SpeedButtonClearCurrentSampleDB: TSpeedButton;
    SpeedButtonSampleNew: TSpeedButton;
    SpeedButtonSampleSelect: TSpeedButton;
    SpeedButtonClose: TSpeedButton;
    procedure ActionApplyExecute(Sender: TObject);
    procedure ActionClearSampleExecute(Sender: TObject);
    procedure ActionFormCloseExecute(Sender: TObject);
    procedure ActionNewSampleDatabaseExecute(Sender: TObject);
    procedure ActionRefreshEditsExecute(Sender: TObject);
    procedure ActionSelectSampleDatabaseExecute(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  FormSelectDatabase: TFormSelectDatabase;
  ConfigFilePath:String;
  Config:TIniFile;

implementation
 uses unitadddatabase, udatamodule, unitfichaamostra;
{$R *.lfm}

{ TFormSelectDatabase }

procedure TFormSelectDatabase.FormCreate(Sender: TObject);
var Aux: String;
begin
  OpenDialog1.Filter:='ALL FILES |  *.s3db; *.sqlite; *.db;';
  Config := TIniFile.Create(Dados.Caminho+'\config.ini');

  Aux:=Config.ReadString('Configurations', 'Language', 'English');
  Edit2.text:=config.ReadString('Configurations','Sample',strDatabase);
  Config.Free;

  if SetLanguage(Aux) then
  begin
    FormSelectDatabase.Caption:=Lang.Database;
    BCLabelSampleDB.Caption:=Lang.CurrentDatabase;
    SpeedButtonSampleSelect.Hint:=Lang.SelectAnalisysDatabase;
    SpeedButtonClearCurrentSampleDB.Hint:=Lang.Clear;
    SpeedButtonSampleNew.Hint:=Lang.CreateAnalisysDatabase;
    SpeedButtonClose.Hint:=Lang.Close;
  end;
end;

procedure TFormSelectDatabase.ActionFormCloseExecute(Sender: TObject);
begin
  Edit2.Text:=Dados.DatabaseSampleFileName;
  FormSelectDatabase.Visible:=False;
end;

procedure TFormSelectDatabase.ActionClearSampleExecute(Sender: TObject);
begin
  Edit2.Text:=EmptyStr;
end;

procedure TFormSelectDatabase.ActionApplyExecute(Sender: TObject);
begin
  if Dados.ChooseDatabase('amostra', Edit2.Text ) then
  begin
    Dados.SetDatabase(Edit2.Text);
    FormFichaAmostra.Preenche_Amostras;
  end
  else
    ShowMessage(Lang.TheSelectedDatabaseIsNotValid);
  FormSelectDatabase.Hide;
end;

procedure TFormSelectDatabase.ActionNewSampleDatabaseExecute(Sender: TObject);
begin
  FormAddDatabase.Tipo:='amostra';
  FormAddDatabase.Show;
end;

procedure TFormSelectDatabase.ActionRefreshEditsExecute(Sender: TObject);
begin
  Edit2.Text:=Dados.DatabaseSampleFileName;
end;

procedure TFormSelectDatabase.ActionSelectSampleDatabaseExecute(Sender: TObject
  );
begin
  if OpenDialog1.Execute then
   if OpenDialog1.FileName <> EmptyStr then
   begin
    if FileExists(OpenDialog1.FileName) then
       if Dados.ChooseDatabase('amostra', OpenDialog1.FileName) then
        begin
          Dados.DatabaseSampleFileName:= OpenDialog1.FileName;
          Edit2.Text:=Dados.DatabaseSampleFileName;
          Config:=TIniFile.Create(Dados.Caminho+'\config.ini');
          Config.WriteString('Database', 'Sample', Dados.DatabaseSampleFileName);
          Config.Free;
        end
        else
          ShowMessage(Lang.TheSelectedDatabaseIsNotValid);
   end
   else
     ShowMessage(Lang.TheSelectedDatabaseIsNotValid);
end;

end.

